/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

#include <assert.h>

#include "common.h"


using namespace Scenic::Renderer;


static ResourcePool canvasPool("Canvas");


Canvas::Canvas(JNIEnv* env, jobject canvas)
: Resource(&canvasPool)
{
    JAWT awt;
    JAWT_DrawingSurface* ds;
    JAWT_DrawingSurfaceInfo* dsi;
    jboolean result;
    jint lock;
	jclass clazz = env->GetObjectClass(canvas);
	
    // Get the AWT
    awt.version = JAWT_VERSION_1_3;
    result = JAWT_GetAWT(env, &awt);
	assert(result != JNI_FALSE);
	
	// Get the drawing surface
	ds = awt.GetDrawingSurface(env, canvas);
	assert(ds != NULL);
	
	// Lock the drawing surface
	lock = ds->Lock(ds);
	assert((lock & JAWT_LOCK_ERROR) == 0);
	
	// Get the drawing surface info
	dsi = ds->GetDrawingSurfaceInfo(ds);
	
	// Get the platform-specific drawing info
#ifdef DIRECTX
	JAWT_Win32DrawingSurfaceInfo* dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
	
	hwnd = dsi_win->hwnd;
	device = new Device(hwnd);
	printf("New Scenic canvas (HWND: %d, id: %d)\n", dsi_win->hwnd, id);
#endif
#ifdef OPT_X11
	JAWT_X11DrawingSurfaceInfo *dsi_x = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
	window = dsi_x->drawable;
	device = new Device(window);
	printf("New Scenic canvas (Window: %d, id: %d)\n", dsi_x->drawable, id);
#endif
	
    // Free the drawing surface info
    ds->FreeDrawingSurfaceInfo(dsi);
	
    // Unlock the drawing surface
    ds->Unlock(ds);
	
    // Free the drawing surface
    awt.FreeDrawingSurface(ds);
	

	printf("Canvas creation done\n");
}

Canvas::~Canvas()
{
	delete device;
}

void Canvas::reset()
{
	device->reset();
}

Canvas *Canvas::get(int id)
{
	return (Canvas*)canvasPool.get(id);
}
